<?php
/*
	我的购物车
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['lang']){
	$_SESSION['show_lang']=$_RQ['lang'];
}
//未登录下单
model('jy_weishop/member')->checklogin();
$return['item']=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
if(!$return['item'] && !$_RQ['isstore']){
	$_SESSION['uid']=0;
	//exi('找不到会员信息！请先登陆！','error','jy_weishop/account.login/');
}
$_RQ['merchid']=$_RQ['merchid']?:$_RQ['onemerchid'];
if($_RQ['merchid'] && !$merchid){
	$merchid=$_RQ['merchid'];
	$_SESSION['onemerchid']=$merchid;
}
$merchid=$merchid?:$_SESSION['merchid'];
$merchid=$merchid?:$_SESSION['onemerchid'];
$myshop=pdo_get('jy_merch_user',array('id'=>$merchid));
if($_RQ['op']=='main'){
	$return['list']=array();
	$getcarts=model('jy_weishop/cart')->getall(1,$myshop['id'],true);
	$carts=$getcarts['list'];
	$allnum=$getcarts['alltotal'];
	$goodsprice=$getcarts['goodsprice'];
	$return['cartnums']=$allnum;
	set_html('title','我的购物车');
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$ismerch=1;
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'merchid'=>$merchid,
	);
	//未登录下单
	if(!$_SESSION['uid']){
		$return['list']=$_SESSION['cartlist']?:array();
		exi($return);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 100;
	$total='';
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=pdo_getlist('jy_weishop_member_cart',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$goods=pdo_get('jy_weishop_goods',array('id'=>$item['goodsid']));
			unset($goods['content']);
			unset($goods['images']);
			if($_SESSION['is_tomedia']){
				$goods['image']=$goods['image']?tomedia($goods['image']):'';
			}
			$item['merchid']=$goods['merchid'];
			//获取活动信息
			$goods['storeid']=$store_id;
			$goods['optionid']=$item['optionid'];
			$goods=model('jy_weishop/activity')->get_upgoods($goods);
			$item['oldprice']=$goods['oldprice'];
			$item['price']=$goods['price'];
			$goods['userbuy'] = 1;
			$goods['levelbuy'] = 1;
			$goods['groupbuy'] = 1;
			$goods['canbuy'] = ($goods['status'] == 1) && empty($goods['deleted']) && ($goods['total'] > 0);
			if (0 < $goods['usermaxbuy']) {
				$order_goodscount = pdo_fetchcolumn('select ifnull(sum(og.total),0)  from ' . tablename('jy_weishop_order_goods') . ' og ' . ' left join ' . tablename('jy_weishop_order') . ' o on og.orderid=o.id ' . ' where og.goodsid=:goodsid and  o.status>=1 and o.mid=:mid  and og.uniacid=:uniacid ', array(':goodsid' => $goods['id'], ':uniacid' => $_SESSION['uniacid'], ':mid' => $_SESSION['uid']));
				if ($goods['usermaxbuy'] <= $order_goodscount) {
					$goods['userbuy'] = 0;
					$goods['canbuy'] = false;
				}
				$goods['order_goodscount']=$order_goodscount;
			}
			if ((0 < $goods['isendtime']) && (0 < $goods['endtime']) && ($goods['endtime'] < time())) {
				$goods['canbuy'] = false;
			}
			$item['kucun']=$goods['total'];
			if($item['optionid']){
				$item['option']=$goods['options'];
				$item['kucun']=$item['option']['stock'];
				$specs = explode("_", $item['option']['specs']);
				$specsall = pdo_getall('jy_weishop_goods_spec_item',array('id'=>$specs,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				if($specsall){
					$optiontitle='';
					foreach($specsall as $k=>&$sp){
						if($k==0){
							$optiontitle .=$sp['title'];
						}else{
							$optiontitle .='，'.$sp['title'];
						}
					}
				}
				$item['optiontitle']= $optiontitle;
				if($item['option']['stock']<1){
					$goods['canbuy'] = false;
				}
			}
			//判断nobuy状态：
			$unit=$goods['unit']?:'件';
			if($goods['userbuy']==0){
				$goods['nobuytitle']='您已经超出最大'.$goods['usermaxbuy'].'件购买量';
			}elseif($goods['levelbuy']==0){
				$goods['nobuytitle']='您当前会员等级没有购买权限';
			}elseif($goods['groupbuy']==0){
				$goods['nobuytitle']='您所在的用户组没有购买权限';
			}elseif($goods['timebuy']==-1){
				$goods['nobuytitle']='未到开始抢购时间!';
			}elseif($goods['timebuy']==1){
				$goods['nobuytitle']='抢购时间已经结束';
			}else{
				$goods['nobuytitle']='商品已售罄';
			}
			if ($goods['usermaxbuy'] && $goods['usermaxbuy'] < $order_goodscount + $item['total']) {
				$goods['outnumtitle']='超购买量,请减少';
				$goods['outtype']=1;
			}
			if($item['kucun']<$item['total']){
				$goods['outnumtitle']='超库存,请减少';
				$goods['outtype']=2;
			}
			if($goods['minbuynum'] && $item['total']<$goods['minbuynum']){
				$goods['outnumtitle']='单次最少购买'.$goods['minbuynum'].$unit;
				$goods['outtype']=3;
			}
			if($goods['maxbuy'] && $item['total']>$goods['maxbuy']){
				$goods['outnumtitle']='单次最多购买'.$goods['minbuynum'].$unit;
				$goods['outtype']=4;
			}
			$sales=model('jy_weishop/activity')->get_sales($goods['id']);
			if($sales['maxnum']>0 && $item['total']>$sales['maxnum']){
				$goods['outnumtitle']='只能购买活动品'.$sales['maxnum'].$unit;
				$goods['outtype']=5;
			}
			if($goods['outtype'] || !$goods['canbuy']){
				pdo_update('jy_weishop_member_cart',array('selected'=>0),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
				$item['selected']=0;
			}
			$goods['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$item['goodsid']),'weight');
			$item=array_merge($goods,$item);
		}
		if($ismerch){
			$nowplugin=$_SESSION['current_plugin']?:'jy_merch';
			$menus_index=$_SESSION['menus_index']?:$nowplugin.'/myshop/';
			$merchs=array();
			$selectmerch=$this->session('selectmerch')?:array();
			foreach($list as $lv){
				$goods=$lv;
				$merchid=$lv['merchid'];
				if($merchid && empty($merchs[$merchid])){
					if(!$selectmerch){
						$selectmerch[$merchid]=$merchid;
					}
					$merch=pdo_get('jy_merch_user',array('id'=>$merchid),array('id','merchname'));
					$merch['coupons']=model('jy_weishop/coupon')->getmerchcoupon($merchid,1);
					$merchs[$merchid]=array(
						'selected'=>$selectmerch[$merchid]?1:0,
						'total'=>0,
						'totalprice'=>0,
						'typename'=>'商户',
						'merchid'=>$merchid,
						'merch'=>$merch,
						'url'=>$_SESSION['merchshop']?$_SESSION['merchshop'].'onemerchid='.$merchid:'jy_merch/myshop//id='.$merchid,
					);
				}
				if(!$merchid && empty($merchs[$merchid])){
					if(!$selectmerch){
						$selectmerch['-1']=1;
					}
					$merch=array('merchname'=>'平台自营','id'=>0);
					$merch['coupons']=model('jy_weishop/coupon')->getmerchcoupon(0,1);
					$merchs[0]=array(
						'selected'=>$selectmerch['-1']?1:0,
						'total'=>0,
						'totalprice'=>0,
						'typename'=>'平台',
						'merchid'=>'-1',
						'merch'=>$merch,
						'url'=>$menus_index,
					);
				}
				$merchs[$merchid]['goods'][]=$goods;
				if($goods['selected']){
					$merchs[$merchid]['total'] +=$goods['total'];
					$merchs[$merchid]['totalprice'] +=$goods['total']*$goods['price'];
				}
			}
			$this->session('selectmerch',$selectmerch);
			$list=$merchs;
		}
	}
	$list=array_values($list);
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='plus'){
	//增加
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
	$cart=pdo_get('jy_weishop_member_cart',array('id'=>$id),array('id','total','goodsid','optionid'));
	model('jy_weishop/goods')->check_buy($cart['goodsid'],intval($cart['total'])+1,intval($cart['optionid']),true,true);
	pdo_update('jy_weishop_member_cart',array('total +='=>1),array('id'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	exi('success');
}
if($_RQ['op']=='minus'){
	//减少
	$post=get_postdata();
	$id=intval($post['id']);
	$goodsid=intval($post['goodsid']);
	$isnotgoodsid=intval($_RQ['isnotgoodsid']);
	$optionid=intval($post['optionid']);
	$merchid=intval($post['merchid']);
	if(!$id && !$goodsid){
		exi('请先选择商品！','error');
	}
	if($id){
		$check=pdo_get('jy_weishop_member_cart',array('id'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('total','goodsid','optionid'));
	}elseif($goodsid){
		$check=pdo_get('jy_weishop_member_cart',array('optionid'=>$optionid,'goodsid'=>$goodsid,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','total','goodsid','optionid'));
		$id=$check['id'];
	}
	if(!$check){
		//return false;
		exi('指定商品不存在！'.$id,'error');
	}
	if($check['total']<=1){
		$this->pdo_delete('member_cart',array('id'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($post['getlist']){
		}elseif($post['wxapp']){
			exi('success');
		}else{
			exi('删除成功!');
		}	
	}elseif($check){
		//减少时不核实，这样可以清空购物车
		//$this->model('goods')->check_buy($check['goodsid'],intval($check['total'])-1,intval($check['optionid']),true);
		pdo_update('jy_weishop_member_cart',array('total -='=>1),array('id'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($post['getlist']){
		}else{
			exi('success');
		}
	}
	$allcart=model('jy_weishop/cart')->getall('',$merchid,$isnotgoodsid);
	//运费计算：
	if($merchid){
		$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid));
	}
	$order=array(
		'list'=>$allcart['list'],
		'total_price'=>$allcart['goodsprice'],
	);
	$up_dispatch=model('jy_weishop/order/dispatch')->up_dispatch($order,array(),'',$_SESSION['store_id'],$merch);
	$return['dispatchprice']=$up_dispatch['get_freight']['fee'];
	$return['list']=$allcart['list'];
	exi($return);
}
if($_RQ['op']=='addtocart'){
	$post=get_postdata();
	$id=intval($post['goodsid']);
	$merchid=intval($post['merchid']);
	$number=intval($post['num']);
	$isnotgoodsid=intval($_RQ['isnotgoodsid']);
	if(!$id){
		exi('选择的商品不存在，请重新选择！','error');
	}
	model('jy_weishop/activity/sales')->check_sale_new(array(array('goodsid'=>$id)));
	if(!$number){
		$number=1;
	}
	$spsids = '';
	if($post['specid']){
		foreach($post['specid'] as $k=>&$s){
			if($k==0){
				$spsids .= $s;
			}else{
				$spsids .= '_'.$s;
			}
		}
		$optionid = pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>$spsids),'id');
	}
	if($post['optionid']){
		$optionid=$post['optionid'];
	}
	$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($store_id){
				$getinfo=model('jy_weishop/store')->getinfo($id,$store_id);
				$goods['total']=$getinfo['id']?$getinfo['total']:$goods['total'];
				$goods['price']=$getinfo['id']?$getinfo['price']:$goods['price'];
				if($optionid){
					$options=model('jy_weishop/store')->getoption($id,$store_id,'',$optionid);
				}
	}
	if(!$options['id'] && $optionid){
		$options = pdo_get('jy_weishop_goods_option',array('goodsid'=>$id,'id'=>$optionid));
	}
	$ifgoods=pdo_get('jy_weishop_member_cart',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id,'optionid'=>$optionid));
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'goodsid'=>$id,
		'price'=>$options['price']>0?$options['price']:$goods['price'],
		'optionid'=>$optionid,
		'selected'=>1,
	);
	if($ifgoods){
		$cartid=$ifgoods['id'];
		$data['total']=$ifgoods['total']+$number;
		model('jy_weishop/goods')->check_buy($data['goodsid'],intval($data['total']),intval($data['optionid']),true,true);
		pdo_update('jy_weishop_member_cart',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$ifgoods['id']));
		if($post['getlist']){
		}else{
			exi('success');
		}
	}else{
		$data['total']=$number;
		$data['storeid']=$store_id;
		model('jy_weishop/goods')->check_buy($data['goodsid'],intval($data['total']),intval($data['optionid']),true,true);
		//如果来自其他商户分销（保存商户ID）
		$data['merchid']=$goods['merchid']?:$this->session('frommerchid');
		pdo_insert('jy_weishop_member_cart',$data);
		$cartid=pdo_insertid();
	}
	$allcart=model('jy_weishop/cart')->getall('',$merchid,$isnotgoodsid);
	//运费计算：
	if($merchid){
		$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid));
	}
	$order=array(
		'list'=>$allcart['list'],
		'total_price'=>$allcart['goodsprice'],
	);
	$up_dispatch=model('jy_weishop/order/dispatch')->up_dispatch($order,array(),'',$_SESSION['store_id'],$merch);
	$return['dispatchprice']=$up_dispatch['get_freight']['fee'];
	$return['list']=$allcart['list'];
	$return['cartid']=$cartid;
	exi($return);
}
if($_RQ['op']=='check_buy'){
	$storeid=intval($_RQ['storeid']);
	$merchid=intval($_RQ['merchid']);
	$sendtype=intval($_RQ['sendtype']);
	$is_dish=intval($_RQ['is_dish']);
	$prams=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'selected'=>1);
	if($merchid){
		if($merchid==-1){
			$prams['merchid']=0;
		}else{
			$prams['merchid']=$merchid;
		}
	}
	$cart_list=pdo_getall('jy_weishop_member_cart',$prams,array('id','goodsid','total','optionid','merchid'));
	if(!$cart_list){
		exi('您的购物车没有选中商品！','error');
	}
	foreach($cart_list as &$cart){
			$goods=pdo_get('jy_weishop_goods',array('id'=>$cart['goodsid']),array('id','title','no_onebuy'));
			if(!$goods){
				exi('指定商品不存在！','error');
			}
			if($goods['no_onebuy']){
				$no_onebuy=true;
				$no_onebuytitle=$goods['title'];
			}else{
				$can_onebuy=true;
			}
			model('jy_weishop/goods')->check_buy($goods['id'],$cart['total'],$cart['optionid']);
	}
	if($no_onebuy && !$can_onebuy){
		exi('['.$no_onebuytitle.']商品，不能单独购买！','error');
	}
	model('jy_weishop/cart')->check_buy($cart_list,'','',true);
	model('jy_weishop/activity/sales')->check_sale_new($cart_list);
	$query='storeid='.$storeid.'&sendtype='.$sendtype.'&is_dish='.$is_dish.'&merchid='.$merchid;
	if($_SESSION['current_plugin']=='jy_weidish' || $_SESSION['current_plugin']=='jy_weishoppei' || $_SESSION['current_plugin']=='jy_weishoppeishop' || $_SESSION['current_plugin']=='jy_weishopscale'){
		if($_SESSION['current_plugin']=='jy_weishoppeishop'){
			exi('','','jy_weishoppei/create/'.$query);
		}elseif($_SESSION['current_plugin']=='jy_weishopscale'){
			exi('','','jy_weishopscale/create/'.$query);
		}else{
			exi('','',$_SESSION['current_plugin'].'/create/'.$query);
		}
	}elseif($_SESSION['ocreateurl']){
		exi('','',$_SESSION['ocreateurl'].$query);
	}else{
		exi('','','jy_merch/order.create//'.$query);
	}
}
if($_RQ['op']=='selectall'){
	$type=intval($_RQ['type']);
	pdo_update('jy_weishop_member_cart',array('selected'=>$type),array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	exi('success');
}
if($_RQ['op']=='select'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
	$check=pdo_get('jy_weishop_member_cart',array('id'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('total'));
	if(!$check){
		exi('指定商品不存在！','error');
	}
	pdo_update('jy_weishop_member_cart',array('selected'=>intval($post['selected'])),array('id'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	exi('success');
}
if($_RQ['op']=='selectmerch'){
	$post=get_postdata();
	$id=intval($post['id']);
	$selectmerch=$this->session('selectmerch')?:array('0'=>1);
	if($selectmerch[$id]){
		unset($selectmerch[$id]);
	}else{
		$selectmerch[$id]=$id;
	}
	$this->session('selectmerch',$selectmerch);
	exi('success');
}
if($_RQ['op']=='offeradd'){
	$sale_offer=model('jy_weishop/activity/sales')->getoffer();
	foreach($sale_offer['list'] as $item){
		if($item['canbuy']){
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'goodsid'=>$item['id'],
				'price'=>$item['oldprice'],
				'optionid'=>$item['optionsid'],
				'selected'=>1,
			);
			$ifgoods=pdo_get('jy_weishop_member_cart',array('goodsid'=>$item['id'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','total'));
			if($ifgoods){
				$data['total']=$ifgoods['total']+1;
				pdo_update('jy_weishop_member_cart',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$ifgoods['id']));
			}else{
				$data['total']=1;
				//如果来自其他商户分销（保存商户ID）
				$data['total']['merchid']=$this->session('frommerchid');
				pdo_insert('jy_weishop_member_cart',$data);
			}
		}
	}
	exi('添加成功！');
}
if($_RQ['op']=='newadd'){
	$sale_new=model('jy_weishop/activity/sales')->getnew();
	foreach($sale_new['list'] as $item){
		if($item['cant_buy']!=1){
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'goodsid'=>$item['id'],
				'price'=>$item['price'],
				'optionid'=>$item['optionsid'],
				'selected'=>1,
			);
			$ifgoods=pdo_get('jy_weishop_member_cart',array('goodsid'=>$item['id'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','total'));
			if($ifgoods){
				$data['total']=$ifgoods['total']+1;
				pdo_update('jy_weishop_member_cart',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$ifgoods['id']));
			}else{
				$data['total']=1;
				//如果来自其他商户分销（保存商户ID）
				$data['total']['merchid']=$this->session('frommerchid');
				pdo_insert('jy_weishop_member_cart',$data);
			}
		}
	}
	exi('添加成功！');
}
if($_RQ['op']=='del'){
	//删除
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	if(count($del)==0){
		exi('请先选择商品','error');
	}
	foreach($del as $id){
		$id=intval($id);
		pdo_delete('jy_weishop_member_cart',array('id'=>$id,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	}
	exi('删除成功！');
}
?>